
from autoTest_brtcApi.core.usercenter_apiUtils import userApiUtils
from autoTest_brtcApi.utils.write import write_yaml
from autoTest_brtcApi.utils.read import readFile
# ##在该模块的方法里面，构造请求头和参数
headers = {"Content-Type": "application/json",
                   "Accept": "application/json"
                   }
class UserApi():
    def reg_api(self,username,password,phone):
        # 请求参数
        params = {
            "user_name": username,
            "passwd": password,
            "phone": phone
        }
        # params = {
        #     "user_name": "wsgggggggggg",
        #     "passwd": "wsg888ggg",
        #     "phone": "18874988666666"
        # }
        # 接受接口返回的数据
        res_reg = userApiUtils.reg_utils(headers=headers,json=params)
        return res_reg

    def login_api(self):
        # 请求参数
        params2 = {
            "passwd": "28a977194d0f5eefb0ea1238b1e31f8c",
            "phone": "188749886666"
        }
        # 接受接口返回的数据
        res_login = userApiUtils.login_utils(headers=headers,json=params2)
        if res_login.json()['code'] == 0:
            # ####把动态变化的userID和token存放到yaml文件
            userId = res_login.json()["data"]["userId"]
            token = res_login.json()["data"]["token"]
            # 将userID和token写进yaml
            content = {"userId": str(userId),
                       "token": token}

            write_yaml('tmp.yaml', content)
            return res_login

    def modifyUserNick(self):
        # 读取tmp.yaml,获取userId和token
        response = readFile("tmp.yaml")
        # 修改昵称
        # 获取userid，token
        headers_modifyUserNick = {"userId": str(response["userId"]),"token":response["token"]}
        params3 = {"nickName": "wsgggggggg"}
        # 接受接口返回的数据
        res_UserNick = userApiUtils.modifyUserNick(headers_modifyUserNick,data=params3)
        return res_UserNick

    # ##################修改昵称
    # def Revise(self):
    #     res2 = self.login()  # 调用login函数并获取返回值
    #     # 获取userid，token
    #     userId = res2.json()["data"]["userId"]
    #     token = res2.json()["data"]["token"]
    #     # print(res2.json())
    #
    #     # # 接口地址
    #     modifyUserNick_login = "/modifyUserNick"
    #     # #拼接注册接口的URL
    #     login_url = self.root_url + modifyUserNick_login
    #     # # 请求头
    #     headers3 = {"userId": str(userId), "token": token}
    #     # # 请求参数
    #     params3 = {"nickName": "wsgggggggg"}
    #     # 发送请求
    #     res_modifyUserNick = req.put(url=login_url, headers=headers3, params=params3)
    #     print(res_modifyUserNick.json())
    #     assert 0 == res_modifyUserNick.json()['code']
    #     assert "修改成功" in res_modifyUserNick.json()['message']
# 实例化userApi
userApi = UserApi()